package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.coyote.Request;

import com.wanmait.hotelManage.dao.PagerDAO;
import com.wanmait.hotelManage.dao.RegisterDAO;
import com.wanmait.hotelManage.dao.RoomDAO;
import com.wanmait.hotelManage.dao.RoomTypeDAO;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.Register;
import com.wanmait.hotelManage.vo.Room;
import com.wanmait.hotelManage.vo.RoomType;

/**
 * Servlet implementation class RoomServlet
 */
@WebServlet("/manage/RoomServlet")
public class RoomServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		if("list".equals(action)) {
			this.list(request, response);
		}else if("insert".equals(action)) {
			this.insert(request, response);
		}else if("beforeupdate".equals(action)) {
			this.beforeupdate(request, response);
		}else if("update".equals(action)) {
			this.update(request, response);
		}else if("del".equals(action)) {
			this.del(request, response);
		}else if("dele".equals(action)) {
			this.dele(request, response);
		}else if("inittwo".equals(action)) {
			this.initTwo(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else if("checkinInformation".equals(action)) {
			this.checkinInformation(request, response);
		}else if("checkinInformationTwo".equals(action)) {
			this.checkinInformationTwo(request, response);
		}else if("number".equals(action)) {
			this.number(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void number(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		Room room=RoomDAO.getRoomdao().findNumber(id);
		request.setAttribute("room", room);
		request.getRequestDispatcher("/manage/room/number.jsp").forward(request, response);
	}
	/**
	 * 显示全部信息的分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void checkinInformationTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Integer no=Integer.parseInt(request.getParameter("pageNo"));
		Pager pager=new Pager();
		pager.setPageNo(no);
		pager.setPageSize(10);
		int rows=RegisterDAO.getRegisterdao().getRoomRows(id);
		pager.setPageCount(rows);
		List<Register>register=RegisterDAO.getRegisterdao().findRoomId(id,pager);
		request.setAttribute("register", register);
		request.setAttribute("pager", pager);
		request.setAttribute("id", id);
		request.getRequestDispatcher("/manage/room/checkinInformationTwo.jsp").forward(request, response);
	}
	/**
	 * 显示全部信息
	 */
	protected void checkinInformation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(10);
		int rows=RegisterDAO.getRegisterdao().getRoomRows(id);
		pager.setPageCount(rows);
		List<Register>register=RegisterDAO.getRegisterdao().findRoomId(id,pager);
		request.setAttribute("register", register);
		request.setAttribute("pager", pager);
		request.setAttribute("id", id);
		request.getRequestDispatcher("/manage/room/checkinInformation.jsp").forward(request, response);
	}
	/**
	 * ajax搜索
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String catid=request.getParameter("catid");
	
	String check=request.getParameter("check");
	String no=request.getParameter("pageNo");
	if(null==no) {
		no="1";
	}
	if(null==catid) {
		catid="0";
	}
	if(null==check) {
		check="0";
	}
	Boolean flag=false;
	if(catid.equals("0")&&check.equals("0")) {
		Pager pager=new Pager();
		System.out.println(no+"111");
		pager.setPageNo(Integer.parseInt(no));
		pager.setPageSize(10);
		Integer rows=PagerDAO.getPagerdao().getrows();
		pager.setPageCount(rows);
//		System.out.println(pager.getStart());
		List<Room>rooms=RoomDAO.getRoomdao().findall(pager);
		request.setAttribute("rooms", rooms);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/room/search0.jsp").forward(request, response);
		return;
	}else if(catid.equals("0")&&check.equals("1")) {
			flag=true;
		Pager pager=new Pager();
		pager.setPageNo(Integer.parseInt(no));
		pager.setPageSize(10);
		Integer rows=PagerDAO.getPagerdao().getrows(flag);
		//System.out.println(rows);
		pager.setPageCount(rows);
		//System.out.println(pager.getStart());
		List<Room>rooms=RoomDAO.getRoomdao().findall(pager,flag);
		request.setAttribute("rooms", rooms);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/room/search1.jsp").forward(request, response);
		return;
	}else if(catid.equals("0")&&check.equals("2")){
		Pager pager=new Pager();
		pager.setPageNo(Integer.parseInt(no));
		pager.setPageSize(10);
		Integer rows=PagerDAO.getPagerdao().getrows(flag);
		//System.out.println(rows);
		pager.setPageCount(rows);
		//System.out.println(pager.getStart());
		List<Room>rooms=RoomDAO.getRoomdao().findall(pager,flag);
		request.setAttribute("rooms", rooms);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/room/search2.jsp").forward(request, response);
		return;
	}else if(check.equals("0")){
		List<Room>rooms=RoomDAO.getRoomdao().findNumberAndIds(Integer.parseInt(catid));
		request.setAttribute("rooms", rooms);
		request.getRequestDispatcher("/manage/room/search.jsp").forward(request, response);
		return;
	}else {
		
		if("1".equals(check)) {
			flag=true;
		}
	List<Room>rooms=RoomDAO.getRoomdao().findNumberAndId(Integer.parseInt(catid),flag);
		
	request.setAttribute("rooms", rooms);
	request.getRequestDispatcher("/manage/room/search.jsp").forward(request, response);
	}
//	response.sendRedirect(request.getContextPath()+"manage/room/search.jsp");
	}
	/**
	 * ajax的页码跳转
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void initTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer pageNo=Integer.parseInt(request.getParameter("pageNo"));
		System.out.println(2);
		Pager pager=new Pager();
		pager.setPageNo(pageNo);
		pager.setPageSize(10);
		Integer rows=PagerDAO.getPagerdao().getrows();
		pager.setPageCount(rows);
		List<Room> rooms=RoomDAO.getRoomdao().findall(pager);
		request.setAttribute("rooms", rooms);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/room/init.jsp").forward(request, response);
	}
	/**
	 * ajax的批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void dele(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		RoomDAO.getRoomdao().dels(id);
//		response.sendRedirect(request.getContextPath()+"/manage/room/list.jsp");
	}
	/**
	 * ajax的删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		RoomDAO.getRoomdao().del(id);
//		response.sendRedirect(request.getContextPath()+"/manage/room/list.jsp");
	}
	/**
	 * 执行更新
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Integer catid=Integer.parseInt(request.getParameter("colId"));
		String check=request.getParameter("check");
		if("空闲".equals(check)) {
			RoomDAO.getRoomdao().update(catid,false,id);
		}else {
			RoomDAO.getRoomdao().update(catid,true,id);
		}
		//System.out.println(catid);
		//System.out.println(id);
		
		response.sendRedirect(request.getContextPath()+"/manage/RoomServlet?action=list");
	}
	/**
	 * 更新之前的准备
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void beforeupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	Integer id=Integer.parseInt(request.getParameter("id"));
	System.out.println(id);
	Integer ids=RoomDAO.getRoomdao().findRoomTypeid(id);
	Boolean idss=RoomDAO.getRoomdao().findcheck(id);
	RoomType roomtype=RoomTypeDAO.getRoomtypedao().findRoomTypeById(ids);
	//Boolean check=RoomDAO.getRoomdao().findcheck(id);
//	List<Room>rooms=RoomDAO.getRoomdao().findall();
	List<RoomType>roomtypes=RoomTypeDAO.getRoomtypedao().findAll();
	request.setAttribute("roomtypes", roomtypes);
	request.setAttribute("roomtype", roomtype);
	request.setAttribute("id", id);
	request.setAttribute("idss", idss);
//	request.setAttribute("rooms", rooms);
	request.getRequestDispatcher("/manage/room/update.jsp").forward(request, response);
	}
	/**
	 * 添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String number=request.getParameter("number");
	Integer chclick=Integer.parseInt(request.getParameter("colId"));
	RoomDAO.getRoomdao().insert(number,chclick);
//	System.out.println(click);
//	RoomType roomtype=RoomTypeDAO.getRoomtypedao().findRoomTypeById(chclick);
//	if(roomtype!=null) {
//		RoomTypeDAO.getRoomtypedao().insert(roomtype);
//	}
	response.sendRedirect(request.getContextPath()+"/manage/RoomServlet?action=list");
	}
	/**
	 * 初始化
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(10);
		Integer rows=PagerDAO.getPagerdao().getrows();
		//System.out.println(rows);
		pager.setPageCount(rows);
		//System.out.println(pager.getStart());
		List<Room>rooms=RoomDAO.getRoomdao().findall(pager);
		List<RoomType>roomtypes=RoomTypeDAO.getRoomtypedao().findAll();
		request.setAttribute("roomtypes", roomtypes);
		request.setAttribute("rooms", rooms);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/room/list.jsp").forward(request, response);
	}
}
